package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.BluetoothState;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.litepal.tablemanager.typechange.BooleanOrm;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GainSettingActivity extends BaseAppCompatActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_head)
    RelativeLayout rlHead;
    @BindView(R.id.iv_low)
    ImageView ivLow;
    @BindView(R.id.rl_low)
    RelativeLayout rlLow;
    @BindView(R.id.iv_high)
    ImageView ivHigh;
    @BindView(R.id.rl_high)
    RelativeLayout rlHigh;
    @BindView(R.id.iv_dac)
    ImageView ivDac;
    @BindView(R.id.rl_dac)
    RelativeLayout rlDac;
    private byte selector;
    private boolean dacIsVisibility;

    @Override
    protected void getBundleExtras(Bundle extras) {
        selector=extras.getByte("gainSetting");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_gain_setting;
    }

    @Override
    protected void initViewsAndEvents() {
        if(selector>10){
            rlDac.setVisibility(View.GONE);
            dacIsVisibility=false;
        }else {
            rlDac.setVisibility(View.VISIBLE);
            dacIsVisibility=true;
        }
        setSelector(selector);
    }
    private void setSelector(byte selector) {
        switch (selector){
            case 1:
                ivLow.setVisibility(View.VISIBLE);
                ivHigh.setVisibility(View.GONE);
                ivDac.setVisibility(View.GONE);
                break;
            case 2:
                ivLow.setVisibility(View.GONE);
                ivHigh.setVisibility(View.VISIBLE);
                ivDac.setVisibility(View.GONE);
                break;
            case 3:
                ivLow.setVisibility(View.GONE);
                ivHigh.setVisibility(View.GONE);
                ivDac.setVisibility(View.VISIBLE);
                break;
            case 11:
                ivLow.setVisibility(View.VISIBLE);
                ivHigh.setVisibility(View.GONE);
                break;
            case 12:
                ivLow.setVisibility(View.GONE);
                ivHigh.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.BLUETOOTH_STATE:
                BluetoothState state = (BluetoothState) eventCenter.getData();
                if (state == null) {
                    return;
                }
                switch (state.getState()) {
                    case Constants.DISCONNECTED:
                        ToastUtils.showLongToast(customApplication, R.string.bluetooth_disconnected);
                        finish();
                        break;
                }
                break;
        }
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.iv_back, R.id.rl_low, R.id.rl_high, R.id.rl_dac})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_low:
                if(dacIsVisibility){
                    setGain((byte) 1);
                }else {
                    setGain((byte) 11);
                }
                break;
            case R.id.rl_high:
                if(dacIsVisibility){
                    setGain((byte) 2);
                }else {
                    setGain((byte) 12);
                }
                break;
            case R.id.rl_dac:
                setGain((byte) 3);
                break;
        }
    }


    private void setGain(byte check){
        selector=check;
        setSelector(selector);
        EventBus.getDefault().postSticky(new EventCenter<>(Constants.GAIN_SETTING, selector));
        ToastUtils.showToast(this, R.string.Set_up_successfully);
        finish();
    }
}
